import axios from "axios";

const request = axios.create({
  baseURL: "http://localhost:3002",
  timeout: 1000,
  method: "POST",
});

const post = ({
  data,
  url,
}) => {
  return request({
    data,
    url,
    transformResponse(data) {
      return JSON.parse(data);
    }
  })
};

export const logon = (data) => {
  return post({
    data,
    url: "logon"
  }).then((res) => {
    return new Promise((resolve, reject) => {
      if (res.status === 200) {
        resolve(res.data);
      } else {
        reject("接口报错");
      }
    });
  });
};

export const login = (data) => {
  return post({
    data,
    url: "login"
  }).then((res) => {
    return new Promise((resolve, reject) => {
      if (res.status === 200) {
        resolve(res.data);
      } else {
        reject("接口报错");
      }
    });
  });
};

export const user = (data) => {
  return post({
    data,
    url: "user"
  }).then((res) => {
    return new Promise((resolve, reject) => {
      if (res.status === 200) {
        resolve(res.data);
      } else {
        reject("接口报错");
      }
    });
  });
};

export const publish = (data) => {
  return post({
    data,
    url: "/publish"
  }).then((res) => {
    return new Promise((resolve, reject) => {
      if (res.status === 200) {
        resolve(res.data);
      } else {
        reject("接口报错");
      }
    });
  });;
};

export const getTypeList = () => {
  return post({
    url: "/type-lists"
  }).then((res) => {
    return new Promise((resolve, reject) => {
      if (res.status === 200) {
        resolve(res.data);
      } else {
        reject("接口报错");
      }
    });
  });;
};

export const getArticle = (data) => {
  return post({
    data,
    url: "/article"
  }).then((res) => {
    return new Promise((resolve, reject) => {
      if (res.status === 200) {
        resolve(res.data);
      } else {
        reject("接口报错");
      }
    });
  });;
};

export const getArticleList = (type) => {
  return post({
    data: {type},
    url: "/article-lists"
  }).then((res) => {
    return new Promise((resolve, reject) => {
      if (res.status === 200) {
        resolve(res.data);
      } else {
        reject("接口报错");
      }
    });
  });;
};